package interfaz;

import automatas.Automata;
import automatas.Validacion;
import javax.swing.JOptionPane;


/**
 * Clase de interfaz para validar una entrada con el expresion regular
 *
 * @author Marco Alvarez
 * @author Sebastian Lena
 */
public class Validar extends javax.swing.JFrame {
    private Automata AFDMin;
    private Validacion afdMinValidacion;
    
    /** Creates new form Validar
     * @param exp Expresion regular
     * @param alf Alfabeto
     * @param a Automata
     */
    public Validar(String exp, String alf, Automata a) {
        this.AFDMin = a;
        initComponents();
        jTextFielExp.setText(exp);
        jTextFieldABC.setText(alf);
        this.focoTXT();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelExp = new javax.swing.JLabel();
        jTextFielExp = new javax.swing.JTextField();
        jLabelABC = new javax.swing.JLabel();
        jTextFieldABC = new javax.swing.JTextField();
        jLabelTxt = new javax.swing.JLabel();
        jTextFieldTxt = new javax.swing.JTextField();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonValidar = new javax.swing.JButton();
        jButtonCerrar = new javax.swing.JButton();

        jPanel1.setBackground(javax.swing.UIManager.getDefaults().getColor("Menu.selectionBackground"));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Validación", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N

        jLabelExp.setText("Expresion Regular:");

        jTextFielExp.setEditable(false);

        jLabelABC.setText("Alfabeto:");

        jTextFieldABC.setEditable(false);

        jLabelTxt.setForeground(new java.awt.Color(250, 250, 250));
        jLabelTxt.setText("Texto a validar:");

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/escoba.png"))); // NOI18N
        jButtonLimpiar.setText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        jButtonValidar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/ok.png"))); // NOI18N
        jButtonValidar.setText("Validar");
        jButtonValidar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonValidarActionPerformed(evt);
            }
        });

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/cerrar.png"))); // NOI18N
        jButtonCerrar.setText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelExp)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFielExp, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelABC)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldABC, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButtonLimpiar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 113, Short.MAX_VALUE)
                        .addComponent(jButtonValidar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonCerrar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelTxt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelExp)
                    .addComponent(jTextFielExp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelABC)
                    .addComponent(jTextFieldABC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelTxt)
                    .addComponent(jTextFieldTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonValidar)
                    .addComponent(jButtonCerrar)
                    .addComponent(jButtonLimpiar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed
        jTextFieldTxt.setText("");
    }//GEN-LAST:event_jButtonLimpiarActionPerformed

    private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButtonCerrarActionPerformed

    private void jButtonValidarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonValidarActionPerformed
        if(validarAFDMin()) {
            JOptionPane.showMessageDialog(this,
                "El texto es válido con la Expresión y el Alfabeto",
                "Válido", JOptionPane.INFORMATION_MESSAGE);
            return;
        } else {
            JOptionPane.showMessageDialog(this,
                "El texto ingresado no es válido con la Expresión y el Alfabeto",
                "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
    }//GEN-LAST:event_jButtonValidarActionPerformed

    /*
     * Al abrir la ventana el foco se coloca en el jTextField para insertar la entrada
     */
    public void focoTXT(){
        jTextFieldTxt.requestFocus();
    }

    /**
     * Metodo para validar la entrada
     * @return true en caso de exito o false en caso contrario
     */
    public boolean validarAFDMin() {
        this.setAfdMinVal(new Validacion(this.jTextFieldTxt.getText(), this.AFDMin));
        boolean exito = this.getAfdMinVal().validar();
        return exito;
    }
    
    /**
     * Obtiene el validador del AFD
     * @return Validacion AFD
     */
    public Validacion getAfdMinVal() {
        return afdMinValidacion;
    }
    
    /**
     * Setea el Validador del AFD
     * @param afdMinValidacion Simulador AFD
     */
    public void setAfdMinVal(Validacion afdMinValidacion) {
        this.afdMinValidacion = afdMinValidacion;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JButton jButtonValidar;
    private javax.swing.JLabel jLabelABC;
    private javax.swing.JLabel jLabelExp;
    private javax.swing.JLabel jLabelTxt;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFielExp;
    private javax.swing.JTextField jTextFieldABC;
    private javax.swing.JTextField jTextFieldTxt;
    // End of variables declaration//GEN-END:variables

}
